package com.itheima.controller;

import com.itheima.dto.PageBean;
import com.itheima.dto.StuQueryRequest;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import com.itheima.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result<Student> page(StuQueryRequest request) {
        PageBean<Student> pageBean=studentService.page(request);
        return Result.success(pageBean);
    }


    @PostMapping
    public Result<String> insert(@RequestBody Student student) {
        studentService.insert(student);
        return Result.success("新增成功");
    }


    @GetMapping("/{id}")
    public Result<Student> getById(@PathVariable Integer id) {
        Student student=studentService.getById(id);
        return Result.success(student);
    }

    @PutMapping
    public Result<String> update(@RequestBody Student student) {
        studentService.update(student);
        return Result.success("修改成功");
    }

    @DeleteMapping("/{ids}")
    public Result<String> deleteByIds(@PathVariable Integer[] ids) {
        studentService.deleteByIds(ids);
        return Result.success("删除成功");
    }

    @PutMapping("/violation/{id}/{score}")
    public Result<String> updateScore(@PathVariable Integer id,@PathVariable Integer score) {
        studentService.updateScore(id,score);
        return Result.success("修改分数成功");
    }
}
